package com.qk.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.ClueDto;
import com.qk.dto.MarkFalseClueDto;
import com.qk.entity.Business;
import com.qk.entity.Clue;
import com.qk.entity.ClueTrackRecord;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.mapper.ClueTrackRecordMapper;
import com.qk.service.ClueService;
import com.qk.utils.CurrentUserHoler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class ClueServiceImpl extends ServiceImpl<ClueMapper, Clue> implements ClueService {
    @Autowired
    private ClueMapper clueMapper;

    @Autowired
    private ClueTrackRecordMapper clueTrackRecordMapper;

    @Autowired
    private BusinessMapper businessMapper;


    @Override
    public PageResult<Clue> listClues(ClueDto clueDto) {
        Page<Clue> page = clueMapper.listClues(new Page<Clue>(clueDto.getPage(), clueDto.getPageSize()), clueDto);
        return new PageResult<>(page.getTotal(),page.getRecords());
    }

    //获取线索详情
    @Override
    public Clue getClueById(Integer id) {
        return clueMapper.getClueById(id);
    }

    //线索跟踪
    @Transactional
    @Override
    public void trackClue(Clue clue){
        clue.setStatus(3);
        clue.setUpdateTime(LocalDateTime.now());
        clueMapper.updateById(clue);

        ClueTrackRecord clueTrackRecord = new ClueTrackRecord();
        clueTrackRecord.setClueId(clue.getId());

        clueTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());

        clueTrackRecord.setSubject(clue.getSubject());
        clueTrackRecord.setLevel(clue.getLevel());
        clueTrackRecord.setRecord(clue.getRecord());
        clueTrackRecord.setNextTime(clue.getNextTime());
        clueTrackRecord.setType(1);
        clueTrackRecord.setCreateTime(LocalDateTime.now());
        clueTrackRecordMapper.insert(clueTrackRecord);
    }


    //伪线索业务逻辑
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void markFalseClue(Integer id, MarkFalseClueDto markFalseClueDto) {
        Clue clue = new Clue();
        clue.setId(id);
        clue.setStatus(4);
        clue.setUpdateTime(LocalDateTime.now());
        clueMapper.updateById(clue);

        ClueTrackRecord clueTrackRecord = new ClueTrackRecord();
        clueTrackRecord.setClueId(id);
        clueTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());
        clueTrackRecord.setType(0);
        clueTrackRecord.setRecord(markFalseClueDto.getRemark());
        clueTrackRecord.setFalseReason(markFalseClueDto.getReason());
        clueTrackRecord.setCreateTime(LocalDateTime.now());
        clueTrackRecordMapper.insert(clueTrackRecord);
    }

    //线索转商机
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void toBusiness(Integer id) {
        Clue clue = clueMapper.selectById(id);
        clue.setStatus(5);
        clue.setUpdateTime(LocalDateTime.now());
        clueMapper.updateById(clue);

        Business business = new Business();
        BeanUtils.copyProperties(clue,business);
        business.setId(null);
        business.setUserId(null);
        business.setStatus(1);
        business.setClueId(id);
        business.setNextTime(null);
        business.setUpdateTime(LocalDateTime.now());
        business.setCreateTime(LocalDateTime.now());
        businessMapper.insert(business);
        Result.success();
    }
    //查询线索池列表，仅展示状态为4的线索列表
    @Override
    public PageResult<Clue> poolList(ClueDto clueDto) {
        Page<Clue> page = clueMapper.poolList(new Page<>(clueDto.getPage(), clueDto.getPageSize()), clueDto);
        return new PageResult<>(page.getTotal(), page.getRecords());

    }
}
